import {
  login,
  applicationList
} from '../../network/interface.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    showModal: false,
    inputValue: "",
    inputMobile: "",
    inputPassword: "",
    homes: []
  },


  // 绑定账号
  login: function () {
    this.showDialogBtn()
  },

  onReady(){
    this.toast = this.selectComponent("#toast");
  },
  /**
   * 隐藏模态对话框
   */
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },

  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function () {
    this.hideModal();
  },


  /**
   * 弹窗
   */
  showDialogBtn: function () {
    this.setData({
      showModal: true,
      inputMobile: "18483678315",
      inputPassword: "123456",
    })
  },

  /**
   * 隐藏模态对话框
   */
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },


  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
    this.hideModal();
    let that = this;
    console.log("===========" + that.data)
    // 获取输入的用户的账户信息

    let mobile = that.data.inputMobile;
    let userPasswd = that.data.inputPassword
    console.log(mobile, userPasswd)

    // 登录账号
    login(parseInt(mobile), parseInt(userPasswd),
      function success(res) {
        if (res["status"] === 200) {
          wx.setStorageSync('userInfo', res["data"])
          wx.showToast({
            title: '登录成功',
          })
          let userInfo = res["data"];
          wx.setStorageSync('userInfo', userInfo);
          that.setData({
            isLogin: true
          })

        } else {
          wx.showToast({
            title: '账号或密码错误',
            icon: "error",
          })
        }
      },
      function fail(res) {
        console.log(res);
      })

  },


  // 获取输入的账号
  idInputChange: function (e) {
    let text = e.detail.value;
    this.setData({
      inputMobile: text
    })
  },

  // 获取输入的密码
  passwdInputChange: function (e) {
    let text = e.detail.value;
    this.setData({
      inputPassword: text
    })
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    let userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      this.setData({
        isLogin: true
      })
    }
    let that = this;
    let params={};
    applicationList(params, function success(res) {
      if (res["status"] === 200) {
        console.log(res["data"]["list"])
        that.setData({
          homes: res["data"]["list"]
        })

      }
    }, function fail(res) {

    })

  },

  onPullDownRefresh() {
    wx.showNavigationBarLoading();
    let that = this;
    that.onShow();
  },
  onLoad() {
    this.setData({
      search: this.search.bind(this)
  })
  },

  search: function (value) {
    console.log("value===>"+value)
    let that = this;
    let params={};
    params.major=value;
    applicationList(params,
      function success(data) {
        if (data["status"] === 200) {
          that.setData({
            homes: data["data"]["list"]
          })
        }
      },
      function fail(res) {
        console.log(res);
      })
  },

 


})